package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Coupon;
import cn.stylefeng.guns.sys.modular.tao.mapper.CouponMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.CouponLeadParam;
import cn.stylefeng.guns.sys.modular.tao.model.params.CouponParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.CouponResult;
import  cn.stylefeng.guns.sys.modular.tao.service.CouponService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-21
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Resource
    public CouponMapper couponMapper;

    @Override
    public void add(CouponParam param){
        Coupon entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CouponParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CouponParam param){
        Coupon oldEntity = getOldEntity(param);
        Coupon newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CouponResult findBySpec(CouponParam param){
        return null;
    }

    @Override
    public List<CouponResult> findListBySpec(CouponParam param){
        return couponMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CouponParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void addCoupon(CouponParam param) {
        couponMapper.addCoupon(param);
    }

    @Override
    public void updateCoupons(CouponParam param) {
        couponMapper.updateCoupons(param);
    }

    @Override
    public List<CouponResult> couponById(int id) {
        return couponMapper.couponById(id);
    }

    private Serializable getKey(CouponParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Coupon getOldEntity(CouponParam param) {
        return this.getById(getKey(param));
    }

    private Coupon getEntity(CouponParam param) {
        Coupon entity = new Coupon();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
